package com.ggh.jinjilive.ui.mine;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.databinding.FragmentMineBinding;
import com.ggh.jinjilive.dialog.SignDialog;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.CommonUtils;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.bumptech.glide.Glide;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

public class MineFragment extends BaseFragment<FragmentMineBinding> implements OnRefreshListener,View.OnClickListener{
    private SignDialog signDialog;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView() {
        super.initView();
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        //签到
        signDialog = new SignDialog(mContext);
        long time = MerchantSession.getInstance().getSignTime();
        if (time > 0) {
            boolean Whether = CommonUtils.isSameDay(time, CommonUtils.getCurrent());
            if (!Whether){
                if(null != signDialog && !signDialog.isShowing()){
                    signDialog.show();
                    MerchantSession.getInstance().setSignTime(CommonUtils.getCurrent());
                }
            }
        }else {
            if(null != signDialog && !signDialog.isShowing()){
                signDialog.show();
                MerchantSession.getInstance().setSignTime(CommonUtils.getCurrent());
            }
        }

    }

    @Override
    public void initData() {
        super.initData();
        //刷新用户信息
        refreshInformation();
    }




    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);

        //头像
        binding.RoundedImageView.setOnClickListener(this);
        //签到
        binding.signIn.setOnClickListener(this);
        //设置
        binding.settingButton.setOnClickListener(this);
        //关注
        binding.followedLayout.setOnClickListener(this);
        //粉丝
        binding.myFansLayout.setOnClickListener(this);
        //直播历史
        binding.liveHistoryLayout.setOnClickListener(this);
        //钻石
        binding.diamondLayout.setOnClickListener(this);
        //家族
        binding.familyLayout.setOnClickListener(this);
        //主播收益
        binding.activityPolicy.setOnClickListener(this);
        //主播报表
        binding.financialReport.setOnClickListener(this);
        //会员
        binding.vipLayout.setOnClickListener(this);
        //排行榜
        binding.leaderBoardLayout.setOnClickListener(this);
        //我的视频
        binding.myVideoLayout.setOnClickListener(this);
        //我的收益
        binding.myBenefitLayout.setOnClickListener(this);
        //等级提升
        binding.myLevelLayout.setOnClickListener(this);
        //个人明细
        binding.myDetailLayout.setOnClickListener(this);
        //个人认证
        binding.myCertificateLayout.setOnClickListener(this);
        //任务接收
        binding.missionLayout.setOnClickListener(this);
        //我的邀请
        binding.invitationLayout.setOnClickListener(this);
        //联系客服
        binding.customServiceLayout.setOnClickListener(this);
        //我的商城
        binding.myMallLayout.setOnClickListener(this);
    }



    /**
     * 刷新用户信息
     */
    public void refreshInformation(){
        //用户信息
        userMobile = MerchantSession.getInstance().getUser();
        if(null != userMobile && null != binding){
            //图标
            Glide.with(binding.RoundedImageView.getContext()).load(userMobile.getHead_portrait()).into(binding.RoundedImageView);
            //装扮
            Glide.with(binding.RoundedImageView1.getContext()).load(userMobile.getHead_frame()).into(binding.RoundedImageView1);
            //V标
            Glide.with(binding.imgVmark.getContext()).load(userMobile.getImg()).into(binding.imgVmark);
            //用户名
            binding.userNameText.setText(userMobile.getNickname());
            //级别
            binding.levelNum.setText(userMobile.getLevel() +"");
            //级别图片
            Glide.with(binding.levelImg.getContext()).load(userMobile.getLevel_img()).into(binding.levelImg);
            //性别
            if (userMobile.getSex() == 1) {
                binding.genderIcon.setImageResource(R.mipmap.ic_male);
            } else {
                binding.genderIcon.setImageResource(R.mipmap.ic_female);
            }
            //ID
            binding.userIdText.setText("ID：" + userMobile.getUser_id());
            //关注
            binding.followed.setText(userMobile.getFocus_num() + "");
            //直播
            binding.liveHistory.setText(userMobile.getLive_num() + "");
            //粉丝
            binding.myFans.setText(userMobile.getFans_num() + "");
            //隐藏主播
            if (userMobile.getIs_anchor_auth() == 1){
                binding.llNanchor.setVisibility(View.VISIBLE);
            }else{
                binding.llNanchor.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        //更新用户信息
        GeneralUtils.mydata(userMobile.getUser_id(), new AddOnClickListener() {
            @Override
            public void onSkipClicked(String sKip) {
                binding.refreshLayout.finishRefresh();
                if(sKip.equals("刷新")){
                    refreshInformation();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.RoundedImageView://头像
                ARouter.getInstance().build(ARouterUtils.CompileDataActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.sign_in://签到
                GeneralUtils.signIn();

                break;
            case R.id.settingButton://设置
                ARouter.getInstance().build(ARouterUtils.SettingActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.vipLayout://会员
                ARouter.getInstance().build(ARouterUtils.MembersActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.diamondLayout://钻石
                ARouter.getInstance().build(ARouterUtils.DiamondActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.familyLayout://家族
                ARouter.getInstance().build(ARouterUtils.RankingListActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.activityPolicy://主播收益
                ARouter.getInstance().build(ARouterUtils.ZhuBoEarningsActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.financialReport://主播报表
                ARouter.getInstance().build(ARouterUtils.ZhuBoStatementActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.leaderBoardLayout://排行榜
                ARouter.getInstance().build(ARouterUtils.RankingBangActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.myVideoLayout://视频管理
                ARouter.getInstance().build(ARouterUtils.VideoGuanLiActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.myBenefitLayout://我的收益
                ARouter.getInstance().build(ARouterUtils.MineEarningsActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.myLevelLayout://等级提升
                ARouter.getInstance().build(ARouterUtils.MineLevelActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.myDetailLayout://个人明细
                ARouter.getInstance().build(ARouterUtils.MineDetailActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.myCertificateLayout://个人认证
                if(userMobile.getIs_anchor_auth() == 0){
                    ARouter.getInstance().build(ARouterUtils.AuthenticationActivity)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            .navigation();
                }else if(userMobile.getIs_anchor_auth() == 1){
                    ToastUtils.make().show("已认证");
                }else if(userMobile.getIs_anchor_auth() == 2){
                    ToastUtils.make().show("申请中");
                }
                break;
            case R.id.missionLayout://任务接收
                ARouter.getInstance().build(ARouterUtils.TaskReceiveActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.invitationLayout://我的邀请
                ARouter.getInstance().build(ARouterUtils.MineInviteActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
                break;
            case R.id.customServiceLayout://联系客服
                Intent intent = new Intent();
                intent.setAction(intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:0576-80273133"));
                startActivity(intent);
                break;
        }
    }

}
